package com.some;

import java.util.ArrayList;
import java.util.Scanner;

public class XiaoHongShu03 {
    int path = Integer.MAX_VALUE;

    public int exist(int[][] board, int n, int k) {
        if (board == null || board[0] == null || board.length == 0 || board[0].length == 0) {
            return -1;
        }
        boolean[][] isVisited = new boolean[board.length][board[0].length];

        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                if (board[i][j] == 1) {//调用函数
                    search(isVisited, board, n, i, j, 0, k);
                }
            }
        }
        return path;
    }

    private boolean search(boolean[][] isVisited, int[][] board, int n, int i, int j, int index, int k) {

        if (i > 0 && j > 0 && i < board.length && j < board[0].length && board[i][j] == n) {
            path = Math.min(index, path);
            return true;
        }
        if (i < 0 || j < 0 || i >= board.length || j >= board[0].length || isVisited[i][j] || index > k) {
            return false;
        }

        isVisited[i][j] = true;
        boolean res = search(isVisited, board, n, i + 1, j, index + 1, k)
                || search(isVisited, board, n, i - 1, j, index + 1, k)
                || search(isVisited, board, n, i, j + 1, index + 1, k)
                || search(isVisited, board, n, i, j - 1, index + 1, k);

        if (!res) {
            isVisited[i][j] = false;
        }
        return res;
    }

    public static void main(String[] args) {
        XiaoHongShu03 xiaoHongShu03 = new XiaoHongShu03();
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int k = sc.nextInt();

        ArrayList<String> list = new ArrayList<>();
        list.add(sc.nextLine());
        while (sc.hasNextLine()) {
            String tmp = sc.nextLine();
            if (tmp.length() == 0) break;
            list.add(tmp);
        }
        list.remove(0);

        int[][] nums = new int[list.size()][m];
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < nums[0].length; j++) {
                String[] split = list.get(i).split(" ");
                nums[i][j] = Integer.parseInt(split[j]);
            }
        }
        System.out.println(xiaoHongShu03.exist(nums, n, k));
    }
}
